package com.originskyseed.platform.user.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.originskyseed.platform.core.tenant.util.TenantCodeUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.util.bean.PrimaryKeyGeneratorUtil;
import com.originskyseed.platform.user.mapper.PlayerRelateLastLoginMapper;
import com.originskyseed.platform.user.model.PlayerRelateLastLogin;
import com.originskyseed.platform.user.service.IPlayerRelateLastLoginService;

/**
 * 玩家最后登录信息表Service接口实现类（OSI_PLAYER_RELATE_LAST_LOGIN）
 * 
 * @author Tangwanghui
 */
@DubboService
@Component("playerRelateLastLoginService")
public class PlayerRelateLastLoginServiceImpl implements IPlayerRelateLastLoginService {

	private static final Logger logger = LoggerFactory.getLogger(PlayerRelateLastLoginServiceImpl.class);

	@Resource
	private PlayerRelateLastLoginMapper playerRelateLastLoginMapper;

	/**
	 * 删除记录
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKey(String id) throws Exception {
		return this.playerRelateLastLoginMapper.deleteByPrimaryKey(id);
	}

	/**
	 * 批量删除记录
	 * 
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKeys(List<String> ids) throws Exception {
		return this.playerRelateLastLoginMapper.deleteByPrimaryKeys(ids);
	}

	/**
	 * 插入记录
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insert(PlayerRelateLastLogin record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.playerRelateLastLoginMapper.insert(record);
	}

	/**
	 * 插入记录（仅非空值字段）
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insertSelective(PlayerRelateLastLogin record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.playerRelateLastLoginMapper.insertSelective(record);
	}

	/**
	 * 插入记录（仅非空值字段）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insertSelective(String tenantCode,PlayerRelateLastLogin record) throws Exception {
		if(tenantCode!=null&&StringUtils.isNotBlank(tenantCode))
		{
			TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
		}
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.playerRelateLastLoginMapper.insertSelective(record);
	}

	/**
	 * 更新记录
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKey(PlayerRelateLastLogin record) throws Exception {
		record.setUpdateTime(new Date());
		return this.playerRelateLastLoginMapper.updateByPrimaryKey(record);
	}

	/**
	 * 更新记录（仅非空值字段）
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKeySelective(String tenantCode,PlayerRelateLastLogin record) throws Exception {
		if(tenantCode!=null&&StringUtils.isNotBlank(tenantCode))
		{
			TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
		}
		try {
			record.setUpdateTime(new Date());
			return this.playerRelateLastLoginMapper.updateByPrimaryKeySelective(record);
		}
		catch (Exception ex)
		{
			logger.error("===updateByPrimaryKeySelective=="+ex.getMessage()+"==record="+record);
		}
		return 1;
	}


	/**
	 * 更新记录（仅非空值字段）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKeySelective(PlayerRelateLastLogin record) throws Exception {
		record.setUpdateTime(new Date());
		return this.playerRelateLastLoginMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * 查询记录
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public PlayerRelateLastLogin selectByPrimaryKey(String id) throws Exception {
		return this.playerRelateLastLoginMapper.selectByPrimaryKey(id);
	}

	/**
	 *  查询记录,根据玩家id
	 *
	 * @param playerId
	 * @return
	 * @throws Exception
	 */
	@Override
	public PlayerRelateLastLogin selectByPlayerId(String tenantCode,String playerId) throws Exception{
		if(tenantCode!=null&&StringUtils.isNotBlank(tenantCode))
		{
			TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
		}
		return selectByPlayerId(playerId);
	}

	/**
	 *  查询记录,根据玩家id
	 *
	 * @param playerId
	 * @return
	 * @throws Exception
	 */
	public PlayerRelateLastLogin selectByPlayerId(String playerId) throws Exception
	{
		return this.playerRelateLastLoginMapper.selectByPlayerId(playerId);
	}

	/**
	 * 查询列表
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<PlayerRelateLastLogin> selectList(PlayerRelateLastLogin record) throws Exception {
		return this.playerRelateLastLoginMapper.selectList(record);
	}

	/**
	 * 分页查询列表
	 * 
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<PlayerRelateLastLogin> selectListByPage(Map<String, Object> params, BasePage<PlayerRelateLastLogin> page) throws Exception {
		params.put("page", page);
		List<PlayerRelateLastLogin> list = this.playerRelateLastLoginMapper.selectListByPage(params);
		if(list!=null&&list.size()>0)
		{
			for(int i=0;i<list.size();i++)
			{
				PlayerRelateLastLogin playerRelateLastLogin=list.get(i);
				Date registerTime=playerRelateLastLogin.getRegisterTime();
				Date loginTime=playerRelateLastLogin.getLoginTime();
				long day=0;
				if(registerTime!=null&&loginTime!=null) {
					day = (loginTime.getTime() - registerTime.getTime()) / (24 * 60 * 60 * 1000);
				}
				playerRelateLastLogin.setRegisterDay((int)day);
			}
		}
		page.setResults(list);
		return page;
	}


	/**
	 * 查询列表
	 *
	 * @param params
	 * @return
	 * @throws Exception
	 */
	public List<PlayerRelateLastLogin> selectListByMap(Map<String, Object> params) throws Exception{
		return this.playerRelateLastLoginMapper.selectListByMap(params);
	}
}